var utils = require('../../utils/utils');
var counter = require('../../components/counter/counter');

var counterOption = {
    data:{
        wiiCounter:{
            data:{
                countNumber:1,//加减器中的初始化数值
                numberUnit:1,//进行加减法运算时的单元数值
                minNumber:1,//加减器的最小值
                maxNumber:10
            }
        }
    }
};
counter = utils.assign(counter,counterOption);
var compSetting = {
    data:{
        wiiActionSheet:{
            counter:counter.data,
            counterPrefix:{
                prefix:'wiiActionSheet.counter'
            },
            data: {
                isShow: false,
                price: 1,
                id: '1',
                hasCart: false,
                cartText:'加入购物车',
                buyText:'立即购买',
                action:'buy',//当只有一个按钮时点击之后的回调
                skuList:[
                    {
                        id:1,
                        name: '中秋礼包1',
                        imgUrl: 'http://p.qpic.cn/automall_pic/0/20161223140141_89862/500'
                    },
                    {
                        id:2,
                        name: '中秋礼包2',
                        imgUrl: 'http://p.qpic.cn/automall_pic/0/20170105153411_93293/500'
                    }
                ],
                choosedSku:{
                        id:1,
                        name: '中秋礼包3',
                        price:88,
                        prePrice:99,
                        imgUrl: 'http://p.qpic.cn/automall_pic/0/20161223140141_89862/500'
                }
            }
        }
    },
    wiiHideActionSheet: function(){
        this.setData({
            'wiiActionSheet.data.isShow':false
        });
    },
    wiiShowActionSheet:function(e){
        var cartText = e.currentTarget.dataset.cartText || '加入购物车';
        var buyText = e.currentTarget.dataset.buyText || '立即购买';
        var hasCart = e.currentTarget.dataset.hasCart == 'true';
        var action = e.currentTarget.dataset.action;
        console.log(cartText,buyText,hasCart)
        this.setData({
            'wiiActionSheet.data.action':action,
            'wiiActionSheet.data.cartText':cartText,
            'wiiActionSheet.data.buyText':buyText,
            'wiiActionSheet.data.hasCart':hasCart,
            'wiiActionSheet.data.isShow':true
        });
    },
    wiiActionChooseTap: function(evt){
        // this.callback.call(this,'actionSheetChoose',evt.target.id,evt.target.dataset.imgurl);
        //actionSheet选择类型
        var choosedSkuId = parseInt(evt.target.id);
        var choosedImgUrl = evt.target.dataset.imgurl;
        var choosedName = evt.target.dataset.name;
        var price = evt.target.dataset.price;
        var stock = evt.target.dataset.stock;
        var minNumber = evt.target.dataset.minnumber;
        var maxNumber = evt.target.dataset.maxnumber;
        var info = evt.target.dataset.info;
        if(!stock){
            // 库存为0时，不可点击
            return;
        }
        this.setData({
            'wiiActionSheet.data.onePrice':evt.target.dataset.price,
            'wiiActionSheet.data.totalPrice':(price*minNumber).toFixed(1),
            'wiiActionSheet.data.choosedSku.id':choosedSkuId,
            'wiiActionSheet.data.choosedSku.name':choosedName,
            'wiiActionSheet.data.choosedSku.imgUrl':choosedImgUrl,
            'wiiActionSheet.data.choosedSku.price':price,
            'wiiActionSheet.data.choosedSku.stock':stock,
            'wiiActionSheet.data.choosedSku.info':info
        });
        this.callback.call(this,'actionSheetTap');
    },
    wiiActionPayTap: function(){
        this.callback.call(this, 'smsBind', () => {
            var countNumber = this.data.wiiActionSheet.counter.wiiCounter.data.countNumber;
            this.setData({
                'wiiActionSheet.data.choosedSku.countNumber':countNumber,
                'wiiActionSheet.data.isShow':false
            });
            var action = this.data.wiiActionSheet.data.action || 'buy';
            console.log(this.data.wiiActionSheet.data.choosedSku)
            this.callback.call(this,'actionSheet',this.data.wiiActionSheet.data.choosedSku,action);
        })
    },
    wiiActionCartTap: function(){
        this.callback.call(this, 'smsBind', () => {
            var countNumber = this.data.wiiActionSheet.counter.wiiCounter.data.countNumber;
            this.setData({
                'wiiActionSheet.data.choosedSku.countNumber':countNumber,
                'wiiActionSheet.data.isShow':false
            });
            var action = this.data.wiiActionSheet.data.action || 'cart';
            console.log(this.data.wiiActionSheet.data.choosedSku)
            this.callback.call(this,'actionSheet',this.data.wiiActionSheet.data.choosedSku,action);
        })
    },
    wiiActionImageTap: function(evt){
        // this.callback.call(this,'actionSheetImage',evt.target.dataset.imgurl);
        //actionSheet头部图片详情
        var imageUrl = evt.target.dataset.imgurl;
        wx.previewImage({
            current: imageUrl,
            urls: [imageUrl]
        });
    }
};
compSetting = utils.assign(compSetting,counter)
console.log(compSetting.data.wiiActionSheet)
module.exports = compSetting;